import Api from '../../utils/Api';
import Config from '../../utils/Config';

// components/main/main.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    searchBackground: '',
    recipeTypeList: [],
    hotRecipeList: [],
    searchText: '',
    isShowLoding:true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async getMainData() {
      // 获取搜索背景图
      let searchBackground = (await wx.cloud.downloadFile({
        fileID: 'cloud://luncai-160ro.6c75-luncai-160ro-1303039787/indexBack.jpg'
      })).tempFilePath;
      this.setData({
        searchBackground
      })
      this.getRecipeTypeList();
      this.getHotRecipeList()
    },
    // 获取分类数据
    async getRecipeTypeList() {
      // 获取前3条
      var recipeTypeList = (await Api.getLimitData({
        tableName: Config.tableName.recipeType,
        skip: 0,
        limit: 3
      })).data;
      this.setData({
        recipeTypeList
      })
    },
    // 获取热门菜谱
    async getHotRecipeList() {
      // Api.filterData()
      // let hotRecipeList = (await Api.getSortData(Config.tableName.recipesTable,{views:-1},0,20)).list;
      // // console.log(hotRecipeList)
      // this.setData({
      //   hotRecipeList
      // })
      wx.cloud.callFunction({
        name: 'getLimitData',
        data: {
          tableName: Config.tableName.recipesTable,
          sort: {
            views: -1
          },
          where:{
            status:1
          },
          skip: 0,
          limit: 20
        },
        success: (res) => {
          var hotRecipeList = res.result.list;
          // console.log(hotRecipeList);
          this.setData({
            hotRecipeList,
            isShowLoding:false
          });
        },
        fail: (err) => {
          console.log(err)
        }
      });
    },
    bindInp() {},
    goSearch(e) {
      let searchText = this.data.searchText;
      // 跳转至搜索列表页 recipeList
      wx.navigateTo({
        url: '../../pages/recipeList/recipeList?type=search&searchText=' + searchText
      });
    },
    tapSearch(e){
      let searchText = this.data.searchText;
      // 跳转搜索
      wx.navigateTo({
          url: '../../pages/recipeList/recipeList?type=search&searchText='+searchText
        });

  }
  },
})